<?php

namespace app\models;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii;
class Message extends ActiveRecord
{
    public static function tableName()
    {
        return '{{message}}';
    }
    /*新建信息*/
    public function createNew($desk_id,$nick_name,$head_img,$message)
    {
        $in_data=array(
            'desk_id'=>$desk_id,
            'nick_name'=>$nick_name,
            'head_img'=>$head_img,
            'message'=>$message,
            'create_time'=>time(),
        );
        return Yii::$app->db->createCommand()->insert('message',$in_data)->execute();
    }

    /*根据ID 删除信息*/
    public function delOne($id)
    {
        $up_data=array(
            'is_del'=>'1',
        );
        $up_where=array(
            'id'=>$id,
        );
        return Yii::$app->db->createCommand()->update('message',$up_data,$up_where)->execute();
    }

    /*根据桌面ID 删除信息*/
    public function delOneByDeskId($desk_id)
    {
        $up_data=array(
            'is_del'=>'1',
        );
        $up_where=array(
            'desk_id'=>$desk_id,
        );
        return Yii::$app->db->createCommand()->update('message',$up_data,$up_where)->execute();
    }

    /*编辑信息 内容*/
    public function editName($id,$message)
    {
        $up_data=array(
            'message'=>$message,
        );
        $up_where=array(
            'id'=>$id,
        );
        return Yii::$app->db->createCommand()->update('message',$up_data,$up_where)->execute();
    }

    /*根据ID 获取单信息*/
    public function getOneById($id)
    {
        $where=array(
            'id'=>$id
        );
        $info=(new Query())
            ->from('message')
            ->where($where)
            ->one();
        if($info)
        {
            $desk_md=new Desk();
            $info['desk_info']=$desk_md->getOneById($info['desk_id']);
        }
        return $info;
    }

    /*根据桌面CODE 最新 审核通过 is_show=1 的信息*/
    public function getOneByDeskCode($desk_code)
    {
        $desk_md=new Desk();
        #桌面信息
        $desk_info=$desk_md->getOneByCode($desk_code);
        if(!$desk_info)
        {
            return false;
        }
        #留言信息
        $offset=0;
        $limit=1;
        $where=array(
            'is_show'=>'1',
            'is_del'=>'0',
            'desk_id'=>$desk_info['id'],
        );
        $list=(new Query())
            ->from('message')
            ->where($where)
            ->orderBy('create_time DESC')
            ->offset($offset)
            ->limit($limit)
            ->all();
        if($list)
        {
            $out_list=array();
            $desk_md=new Desk();
            foreach ($list as $temp)
            {
                $temp['desk_info']=$desk_md->getOneById($temp['desk_id']);
                $out_list[]=$temp;
            }
            $list=$out_list;
        }
        #存在审核通过信息
        if(isset($list[0]))
        {
            return $list[0];
        }
        else
        {
            return false;
        }



    }

    /*获取信息列表*/
    public function getAll($offset=null,$limit=null)
    {
        $offset=is_null($offset)?0:$offset;
        $limit=is_null($limit)?5:$limit;
        $list=(new Query())
            ->from('message')
            ->where(array('is_del'=>'0'))
            ->orderBy('create_time DESC')
            ->offset($offset)
            ->limit($limit)
            ->all();
        if($list)
        {
            $out_list=array();
            $desk_md=new Desk();
            foreach ($list as $temp)
            {
                $temp['desk_info']=$desk_md->getOneById($temp['desk_id']);
                $out_list[]=$temp;
            }
            $list=$out_list;
        }

        return $list;
    }

    /*根据桌面ID 获取信息列表*/
    public function getListByDeskId($desk_id,$offset=null,$limit=null)
    {
        $offset=is_null($offset)?0:$offset;
        $limit=is_null($limit)?5:$limit;
        $where=array(
            'is_del'=>'0',
            'desk_id'=>$desk_id,
        );
        $list=(new Query())
            ->from('message')
            ->where($where)
            ->orderBy('create_time DESC')
            ->offset($offset)
            ->limit($limit)
            ->all();
        if($list)
        {
            $out_list=array();
            $desk_md=new Desk();
            foreach ($list as $temp)
            {
                $temp['desk_info']=$desk_md->getOneById($temp['desk_id']);
                $out_list[]=$temp;
            }
            $list=$out_list;
        }

        return $list;
    }

    /*获取信息 计数*/
    public function getCount($where=null)
    {
        if(is_null($where))
        {
            $count=(new Query())
                ->from('message')
                ->count();
        }
        else
        {
            $count=(new Query())
                ->from('message')
                ->where($where)
                ->count();
        }
        return $count;

    }

    /*信息 确认通过审核*/
    public function confirmMsg($id)
    {
        $up_data=array(
            'is_show'=>'1',
        );
        $up_where=array(
            'id'=>$id,
        );
        return Yii::$app->db->createCommand()->update('message',$up_data,$up_where)->execute();
    }
}
